package com.dykj.apps.web.sysmanager.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;

import com.dykj.apps.web.core.BaseAction;
import com.dykj.apps.web.sysmanager.dto.FunctionDTO;
import com.dykj.apps.web.sysmanager.dto.GroupInfoDTO;
import com.dykj.apps.web.sysmanager.dto.SysUserDTO;
import com.dykj.apps.web.sysmanager.service.IFunctionBS;
import com.dykj.apps.web.sysmanager.service.IGroupInfoBS;
import com.dykj.apps.web.sysmanager.service.ILogBS;
import com.dykj.platform.framework.exception.ApplicationException;

public class LogAction extends BaseAction {
	private static final long serialVersionUID = 1L;

	private ILogBS bs;

	private IFunctionBS funbs;

	private String username;

	private String password;

	private String checkcoder;

	private IGroupInfoBS groupbs;

	/**
	 * 登陆功能
	 * 
	 * @return
	 * @throws ApplicationException
	 */
	public String login() throws ApplicationException {
		SysUserDTO user = null;
		if(null==username||null==password||"".equals(username)||"".equals(password)){
			alert("用户名或密码不能为空！");
			return execute();
		}else{
			user = bs.login(username, password);
		}
		String result = "success";
		String ss = String.valueOf((this.getSession().getAttribute("checkcoder")));
		if(null==ss||!ss.equals(checkcoder)){
			return execute();
		}else if(null == user) {
			alert("用户名错误！");
			result = "userLogin";
		} else if (!password.equals(user.getPasswd())) {
			alert("密码错误！");
			result = "userLogin";
		} else {
			Map<String, Object> map = new HashMap<String, Object>();
			List list = new ArrayList();
			List mainMenu = funbs.getFuntion(null, user.getRoleid(), user
					.getType());
			Iterator it = mainMenu.iterator();
			while(it.hasNext()){
				FunctionDTO dto = (FunctionDTO)it.next();
				if("1".equals(dto.getType())){
					it.remove();
				}
			}
			map.put("username", username);
			map.put("userRealName", user.getOperatorname());
			map.put("userFunction", list);
			map.put("roleid", user.getRoleid());
			map.put("type", user.getType());
			map.put("groupid", user.getGroupid());
			map.put("mainMenu", mainMenu);
			GroupInfoDTO groupdto = new GroupInfoDTO();
			groupdto = groupbs.getGroupInfoDTO(user.getGroupid());
			if ("0".equals(user.getGroupid())) {
				map.put("groupname", "超级管理员");
			} else {
				map.put("groupname", groupdto.getName());
			}
			map.put("authid", user.getAuthid());
			if ("0".equals(user.getType()) || "1".equals(user.getType())) {
				map.put("treeName", "后台管理");
			} else {
				map.put("treeName", "功能列表");
			}
			map.put("org", groupdto.getOrg());//所属企业社保编号
			this.getSession().setAttribute("userinfo", map);
			this.getSession().setAttribute("checkcoder", "0");
		}
		return result;
	}

	public String getFunction(String parentid) throws ApplicationException {
		return "success";
	}

	/**
	 * 初始化登陆
	 */
	public String execute() throws ApplicationException {
		checkcoder = randomChars(4);
		this.getSession().setAttribute("checkcoder", checkcoder);
		return "userLogin";
	}

	/**
	 * 注销功能
	 * 
	 * @return
	 */
	public String logout() throws ApplicationException {
		this.getSession().removeAttribute("userinfo");
		return execute();
	}

	/**
	 * 生成随机字符串. <br>
	 * 随机字符串的内容包含[0-9]的字符. <br>
	 * 
	 * @param randomLength
	 *            随机字符串的长度
	 * @return 随机字符串.
	 */
	public static String randomChars(int randomLength) {
		char[] randoms = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
		Random random = new Random();
		StringBuffer ret = new StringBuffer();
		for (int i = 0; i < randomLength; i++) {
			ret.append(randoms[random.nextInt(randoms.length)]);
		}
		random = null;
		return ret.toString();
	}

	/*
	 * ================================get
	 * set方法=====================================
	 */
	public ILogBS getBs() {
		return bs;
	}

	public void setBs(ILogBS bs) {
		this.bs = bs;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public IGroupInfoBS getGroupbs() {
		return groupbs;
	}

	public void setGroupbs(IGroupInfoBS groupbs) {
		this.groupbs = groupbs;
	}

	@Override
	public Object getModel() {
		// TODO Auto-generated method stub
		return null;
	}

	public IFunctionBS getFunbs() {
		return funbs;
	}

	public void setFunbs(IFunctionBS funbs) {
		this.funbs = funbs;
	}

	public String getCheckcoder() {
		return checkcoder;
	}

	public void setCheckcoder(String checkcoder) {
		this.checkcoder = checkcoder;
	}
}
